package com.souku.finance.application.ui.lock;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.TextView;

import com.souku.finance.application.R;
import com.souku.finance.application.utils.Constant;
import com.souku.finance.application.utils.ShareUtil;
import com.souku.finance.application.utils.ToastUtil;
import com.souku.finance.application.widget.lock.LockPatternView;

import java.util.List;


public class LockSetupActivity extends Activity implements
        LockPatternView.OnPatternListener {

    private LockPatternView lockPatternView;
    private String password;
    private CountDownTimer timer;
    private static final int STEP_1 = 1; // 开始
    private static final int STEP_2 = 2; // 第一次设置手势完成
    private static final int STEP_3 = 3; // 按下继续按钮
    private static final int STEP_4 = 4; // 第二次设置手势完成
    // private static final int SETP_5 = 4; // 按确认按钮
    private int step;
    private List<LockPatternView.Cell> choosePattern;
    private boolean confirm = false;
    private TextView tv_dec;
    private TextView tv_handgesture_desc;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_please_handgesture);
        initView();
        timer = new CountDownTimer(500, 500) {
            @Override
            public void onTick(long millisUntilFinished) {

            }

            @Override
            public void onFinish() {
                updateView();
            }
        };
        lockPatternView = (LockPatternView) findViewById(R.id.lock_pattern_set);
        lockPatternView.setOnPatternListener(this);
        step = STEP_1;
        updateView();
    }

    private void updateView() {
        switch (step) {
            case STEP_1:
                choosePattern = null;
                confirm = false;
                lockPatternView.clearPattern();
                lockPatternView.enableInput();
                break;
            case STEP_2:
                lockPatternView.disableInput();
                break;
            case STEP_3:
                lockPatternView.clearPattern();
                lockPatternView.enableInput();
                break;
            case STEP_4:
                if (confirm) {
                    lockPatternView.disableInput();
                } else {
                    lockPatternView.setDisplayMode(LockPatternView.DisplayMode.Wrong);
                    lockPatternView.enableInput();
                }

                break;

            default:
                break;
        }
    }

    @Override
    public void onPatternStart() {
        tv_handgesture_desc.setVisibility(View.INVISIBLE);
    }

    @Override
    public void onPatternCleared() {

    }

    @Override
    public void onPatternCellAdded(List<LockPatternView.Cell> pattern) {

    }

    @Override
    public void onPatternDetected(List<LockPatternView.Cell> pattern) {
        if (pattern.size() < LockPatternView.MIN_LOCK_PATTERN_SIZE) {
            tv_handgesture_desc.setVisibility(View.VISIBLE);
            tv_handgesture_desc.setText("至少连接4个点，请重试");
            lockPatternView.setDisplayMode(LockPatternView.DisplayMode.Wrong);
            timer.start();
            return;
        }
        if (password == null) {
            tv_handgesture_desc.setVisibility(View.VISIBLE);
            tv_handgesture_desc.setText("再次绘制");
            password = toStr(pattern);
            step = STEP_3;
            timer.start();
            return;
        }
        if (toStr(pattern).equals(password)) {
            ToastUtil.show("设置成功");
            ShareUtil.getInstance().putExtra(Constant.GesturePass, password);
//            if(ShareUtil.getInstance().getString(Constant.RealStatus).equals("1")||ShareUtil.getInstance().getBoolean(Constant.IsChanleReal)){
//                ShareUtil.getInstance().remove(Constant.IsChanleReal);
//                finish();
//            }else{
//                dialog= DialogUtil.getTipRealDialog(this, new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        switch (v.getId()) {
//                            case R.id.ox:
//                                dialog.dismiss();
//                                break;
//                            case R.id.btn_real:
//                                Intent intent = new Intent(LockSetupActivity.this, RealNameActivity.class);
//                                startActivity(intent);
//                                dialog.dismiss();
//                                break;
//                        }
//                    }
//                });
//                dialog.show();
//            }
            finish();
        } else {
            tv_handgesture_desc.setVisibility(View.VISIBLE);
            tv_handgesture_desc.setText("两次绘制不一致,重新绘制");
            lockPatternView.setDisplayMode(LockPatternView.DisplayMode.Wrong);
            password = null;
            step = STEP_1;
            timer.start();
        }
    }

    private Dialog dialog;


    private String toStr(List<LockPatternView.Cell> pattern) {
        String str = "";

        for (int i = 0; i < pattern.size(); i++) {
            int row = pattern.get(i).getRow();
            int column = pattern.get(i).getColumn();
            switch (row) {
                case 0:
                    if (column == 0) {
                        str += "1";
                    } else if (column == 1) {
                        str += "2";
                    } else if (column == 2) {
                        str += "3";
                    }
                    break;
                case 1:
                    if (column == 0) {
                        str += "4";
                    } else if (column == 1) {
                        str += "5";
                    } else if (column == 2) {
                        str += "6";
                    }
                    break;
                case 2:
                    if (column == 0) {
                        str += "7";
                    } else if (column == 1) {
                        str += "8";
                    } else if (column == 2) {
                        str += "9";
                    }
                    break;
            }
        }
        return str;
    }

    private void initView() {
        tv_dec = (TextView) findViewById(R.id.tv_dec);
        tv_handgesture_desc = (TextView) findViewById(R.id.tv_handgesture_desc);
    }

    @Override
    public void onBackPressed() {

    }
}
